{extend name="public:base"}
{block name="body"}
<body class="pear-container">
	<div class="layui-card">
		<div class="layui-card-body">
			<form class="layui-form" action="">
				<div class="layui-form-item layui-inline">
                    <label class="layui-form-label">搜索关键字</label>
                    <div class="layui-input-inline">
                        <input type="text" name="keywords" autocomplete="off" value="{:input('keywords')}" placeholder="请输入关键字" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit="table-query"
                    lay-filter="table-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <a href="{:url('index')}" class="pear-btn pear-btn-md" >
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </a>
                </div>
			</form>
		</div>
	</div>
	<div class="layui-card">
		<div class="layui-card-body">
			<table id="table-list" lay-filter="table-list"></table>
		</div>
	</div>
</body>
{/block}
{block name="custome"}
	<script type="text/html" id="table-toolbar">
		<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>
        <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
        <button class="pear-btn pear-btn-default pear-btn-md" lay-event="expandAll">
            <i class="layui-icon layui-icon-spread-left"></i>
            展开
        </button>
        <button class="pear-btn pear-btn-default pear-btn-md" lay-event="foldAll">
            <i class="layui-icon layui-icon-shrink-right"></i>
            折叠
        </button>
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="send">
            <i class="layui-icon layui-icon-upload-circle"></i>
            推送至公众号
        </button>
	</script>
	<script type="text/html" id="table-bar">
		<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit" title="编辑"><i class="layui-icon layui-icon-edit"></i></button>
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove" title="删除"><i class="layui-icon layui-icon-delete"></i></button>
	</script>
{/block}
{block name="script"}
    <script type="text/html" id="table-type">
        {{#if (d.type == '0') { }}
        <span>菜单栏</span>
        {{# }else if(d.type == '1'){ }}
        <span>链接</span>
        {{# }else if(d.type == '2'){ }}
        <span>扫码</span>
        {{# } }}
    </script>
    <script type="text/html" id="table-status">
        <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="table-status"  {{ d.status == 1 ? 'checked' : '' }} />
    </script>
    <script type="text/html" id="create_time">
            {{layui.util.toDateString(d.create_time, 'yyyy-MM-dd HH:mm')}}
    </script>
    <script type="text/html" id="icon">
        <div class="icon-xl layui-font-blue">
            <i class="layui-icon {{d.icon}}"></i>
        </div>
    </script>
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'treetable'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let treetable = layui.treetable;
        let treenote;
        let cols = [
            [{
                type: 'checkbox'
            },
            {
                title: '菜单标题',
                field: 'title',
                align: 'center',
                width: 200
            },
            {
                title: '菜单类型',
                field: 'type',
                align: 'center',
                templet:'#table-type'
            },
            {
                title: '创建时间',
                field: 'create_time',
                align: 'center',
                templet: '#create_time'

            },
            
            {
                title: '操作',
                toolbar: '#table-bar',
                align: 'center',
                width: 130
            }
            ]
        ]
    window.render = function(){
        treenote =  treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            skin:'line',
            treeDefaultClose: true,
            elem: '#table-list',
            url: '{:url("index")}',
            parseData: function(res){ 
            return {
            "code": res.code, //解析接口状态
            "msg": res.message, //解析提示文本
            "count": res.data.total, //解析数据长度
            "data": res.data.data //解析数据列表
            }},
            page: false,
            cols: cols,
            skin: 'line',
            toolbar: '#table-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter']
        });
    }
    render();
        form.on('submit(table-query)', function (data) {
            console.log(data);
            let keyword =  data.field.keywords;
            treetable.search('#table-list',keyword);
            return false;
        });
        table.on('tool(table-list)', function (obj) {
            if (obj.event === 'remove') {
                common.remove(obj,"{:url('batch')}");
            }
            if (obj.event === 'edit') {
                common.modalo('{:url("edit")}?ids=' + obj.data['id'],'编辑','560px');
            }
            
        });

        table.on('toolbar(table-list)', function (obj) {
            if (obj.event === 'add') {
                common.modalo('{:url("add")}','新增','560px');
            } else if (obj.event === 'refresh') {
                common.refresh();
            } else if (obj.event === 'batchRemove') {
                common.batchRemove(obj,"{:url('batch')}");
            } else if(obj.event === 'expandAll'){
				treetable.expandAll("#table-list");
			} else if(obj.event === 'foldAll'){
				treetable.foldAll("#table-list");
			} else if(obj.event === 'send'){
                $.ajax({
                url: '{:url("send")}',
                data: {},
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function(result) {
                    if (result.code == 1) {
                        layer.msg('推送成功', {
                            icon: 1,
                            time: 1000
                        }, function() {
                            common.refresh();
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            })
            return false;
            }
        });
    })
</script>
{/block}